<template>
    <div class="personalCenter" style="height:400px;margin-bottom:-150px">

        <el-card class="card" style="width:1600px;height: 800px; margin-left:-390px; margin-top:-20px;">
            <h2>个人资料</h2>
            <div>
                <h3>头像:</h3><el-avatar :src="avatarUrl" style="width:100px;height:100px"></el-avatar>
            </div><br />
            <h3 style="margin-top:-20px;">账户名:{{ user.username }}</h3><br />
            <h3 style="margin-top:-20px;">账号创建时间:{{ create_time }}</h3><br />
            <h3 style="margin-top:-20px;">用户组:{{ user.user_group }}</h3><br />
            <h3 style="margin-top:-20px;">昵称:{{ user.nickname }}</h3><br />
            <h3 style="margin-top:-20px;">邮箱:{{ user.email }}&emsp;邮箱状态:{{ user.email_state === 0 ? "未认证" : "已认证" }}</h3>
            <br />
            <h3 style="margin-top:-20px;">电话:{{ user.phone }}&emsp;电话状态:{{ user.phone_state === 0 ? "未认证" : "已认证" }}</h3>
            <br />
            <span v-show="!isChangeAvatar" style="color:red">上传头像图片只能是 JPG 格式,图片大小不能超过 5MB</span>
            <el-upload class="avatar-uploader" action="http://127.0.0.1:5000/api/user/upload" :headers="headers"
                :data="params" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
                v-show="!isChangeAvatar">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" style="background-color:rgb(220, 233, 233)"></i>
            </el-upload>
            <input v-show="!isChangeNickName" :placeholder="user.nickname" maxlength=" 12" minlength="2"
                v-model="newNickName" style="width: 200px;height:20px;margin-bottom:10px" /><br />
            <el-button v-show="!isChangeNickName || !isChangeAvatar" @click="doNotUpdate">返回</el-button>
            <el-button v-show="!isChangeNickName" @click="confirmUpdateNickName(newNickName)">确认修改</el-button>
            <el-button @click="ChangeAvatar" v-show="isChangeAvatar && isChangeNickName">修改头像</el-button>
            <el-button @click="updateNickName" v-show="isChangeNickName && isChangeAvatar">修改昵称</el-button>
        </el-card>

    </div>
</template>
<script>
import axios from 'axios';
import dateTime from '@/utils/date_time';
export default {
    data() {
        return {
            user: [],
            avatarUrl: '',
            uploadAvatarUrl: '',
            create_time: null,
            isChangeNickName: true,
            isChangeAvatar: true,
            newNickName: null,
            imageUrl: '',
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
            params: {

            }
        }
    },
    methods: {
        handleAvatarSuccess(res, file) {
            this.$message.success("上传头像成功!");
            this.imageUrl = URL.createObjectURL(file.raw);
            let that = this;
            console.log(res.result.url);
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/user/set?user_id=' + that.user.user_id,
                data: {
                    avatar: res.result.url
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    window.location.reload();
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 5;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 5MB!');
            }
            return isJPG && isLt2M;
        },

        updateNickName() {
            this.isChangeNickName = !this.isChangeNickName;
            // this.isChangeAvatar = !this.isChangeAvatar;
        },
        ChangeAvatar() {
            this.isChangeAvatar = !this.isChangeAvatar;
            // this.isChangeNickName = !this.isChangeNickName;
        },
        doNotUpdate() {
            this.isChangeNickName = true;
            this.isChangeAvatar = true;
        },
        confirmUpdateNickName(nickname) {
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/user/set?user_id=' + that.user.user_id,
                data: {
                    nickname: nickname
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    window.location.reload();
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        }
    },
    created: function () {
        if (!window.localStorage.getItem("token")) {
            this.$router.push({ path: '/login' }, () => { }, () => { });
            return;
        }
        let that = this;
        axios.get('http://127.0.0.1:5000/api/user/get_obj', {
            params: {
                username: window.localStorage.getItem("username")
            }
        })
            .then(function (response) {
                console.log(response.data.result.obj);
                that.user = response.data.result.obj;
                that.create_time = dateTime.dateTime(that.user.create_time).format;
                if (response.data.result.obj.avatar) {
                    let url = response.data.result.obj.avatar.split('/');
                    that.avatarUrl = 'http://127.0.0.1:5000/' + url[1] + "/" + url[2] + "/avatar/" + url[3];
                }
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            });

    },
}
</script>
<style lang="less" scoped>
.card {
    position: absolute;
    top: 100px;
    left: 700px;
    width: 900px;
    height: 500px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #5da045;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background-color: #196abb;
}

.avatar-uploader .el-upload:hover {
    border-color: #196abb;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #5d779a;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>